package com.wingice.system.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wingice.system.entity.SysAttachment;
import com.wingice.system.entity.SysAttachmentRelation;
import com.wingice.system.mapper.SysAttachmentMapper;
import com.wingice.system.service.FileUploadService;
import com.wingice.system.service.SysAttachmentRelationService;
import com.wingice.system.service.SysAttachmentService;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 资源信息(SysAttachment)表服务实现类
 */
@Service("sysAttachmentService")
public class SysAttachmentServiceImpl extends ServiceImpl<SysAttachmentMapper, SysAttachment> implements SysAttachmentService {
    @Resource
    private FileUploadService fileUploadService;
    @Resource
    private SysAttachmentRelationService attachmentRelationService;

    @Override
    public List<SysAttachment> getAttachmentList(String module, String keyId) {
        if (StrUtil.isBlank(module) || StrUtil.isBlank(keyId)) {
            return new ArrayList<>();
        }
        List<String> attachmentIdList = attachmentRelationService.list(Wrappers.lambdaQuery(SysAttachmentRelation.class)
                .eq(SysAttachmentRelation::getModule, module)
                .eq(SysAttachmentRelation::getKeyId, keyId)
        ).stream().map(SysAttachmentRelation::getAttachmentId).collect(Collectors.toList());
        if (CollectionUtil.isEmpty(attachmentIdList)) {
            return new ArrayList<>();
        }
        return list(Wrappers.lambdaQuery(SysAttachment.class).in(SysAttachment::getId, attachmentIdList));
    }

    @Override
    public SysAttachment getAttachment(String module, String keyId) {
        if (StrUtil.isBlank(module) || StrUtil.isBlank(keyId)) {
            return null;
        }
        SysAttachmentRelation attachmentRelation = attachmentRelationService.getOne(Wrappers.lambdaQuery(SysAttachmentRelation.class)
                .eq(SysAttachmentRelation::getModule, module)
                .eq(SysAttachmentRelation::getKeyId, keyId)
        );
        if (ObjectUtil.isNull(attachmentRelation)) {
            return null;
        }
        return getOne(Wrappers.lambdaQuery(SysAttachment.class).eq(SysAttachment::getId, attachmentRelation.getAttachmentId()));
    }

    @Override
    public Boolean saveAttachment(String module, String keyId, String attachmentId) {
        final List<String> oldProAttachmentIdList = attachmentRelationService.list(Wrappers.<SysAttachmentRelation>lambdaQuery()
                        .eq(SysAttachmentRelation::getKeyId, keyId)
                        .eq(SysAttachmentRelation::getModule, module))
                .stream().map(SysAttachmentRelation::getAttachmentId)
                .collect(Collectors.toList());

        // 删除关联
        // 过滤出不在
        List<String> deleteAttachmentIdList = oldProAttachmentIdList.stream().filter(item -> !attachmentId.equals(item)).collect(Collectors.toList());
        // 删除文件
        if (!CollectionUtil.isEmpty(deleteAttachmentIdList)) {
            attachmentRelationService.remove(Wrappers.lambdaQuery(SysAttachmentRelation.class).in(SysAttachmentRelation::getAttachmentId, deleteAttachmentIdList));
            deleteAttachmentIdList.forEach(id -> fileUploadService.deleteSingleFile(id));
        }
        //找出新增的
        if (!deleteAttachmentIdList.contains(attachmentId)) {
            final SysAttachmentRelation attachmentRelation = new SysAttachmentRelation();
            attachmentRelation.setKeyId(keyId);
            attachmentRelation.setAttachmentId(attachmentId);
            attachmentRelation.setModule(module);
            attachmentRelationService.save(attachmentRelation);
        }
        return true;
    }

    @Override
    public Boolean saveAttachmentList(String module, String keyId, List<String> attachmentIdList) {
        // 查原项目下指定key的所有附件
        final List<String> oldProAttachmentIdList = attachmentRelationService.list(Wrappers.<SysAttachmentRelation>lambdaQuery()
                        .eq(SysAttachmentRelation::getModule, module)
                        .eq(SysAttachmentRelation::getKeyId, keyId))
                .stream().map(SysAttachmentRelation::getAttachmentId)
                .collect(Collectors.toList());
        // 删除关联
        // 过滤出不在
        List<String> deleteAttachmentIdList = oldProAttachmentIdList.stream().filter(item -> !attachmentIdList.contains(item)).collect(Collectors.toList());
        if (!CollectionUtil.isEmpty(deleteAttachmentIdList)) {
            attachmentRelationService.remove(Wrappers.lambdaQuery(SysAttachmentRelation.class).in(SysAttachmentRelation::getAttachmentId, deleteAttachmentIdList));
            // 删除文件
            deleteAttachmentIdList.forEach(id -> fileUploadService.deleteSingleFile(id));
        }
        //找出新增的
        final List<SysAttachmentRelation> attachmentRelationList = new ArrayList<>();
        attachmentIdList.stream().filter(item -> !oldProAttachmentIdList.contains(item)).forEach(id -> {
            final SysAttachmentRelation attachmentRelation = new SysAttachmentRelation();
            attachmentRelation.setKeyId(keyId);
            attachmentRelation.setAttachmentId(id);
            attachmentRelation.setModule(module);
            attachmentRelationList.add(attachmentRelation);
        });
        if (!CollectionUtils.isEmpty(attachmentRelationList)) {
            attachmentRelationService.saveBatch(attachmentRelationList);
        }
        return true;
    }
}
